<template>
  <div class="content-container eheadmesg environmentchart">
    <div class=" auto-center main" v-if="pagedata">
      <chart :option="chartoption" :id="'envirchart'"></chart>
      <div class="data-title">
        <span class="data-title-text">震中海拔</span>
          <span class="data-title-text"><span>{{pagedata.option}}</span>米</span>
      </div>
    </div>
      <!-- <chart :option="chartoption" :id="'envirchart'"></chart> -->
  </div>
</template>

<script>
import ECharts from "vue-echarts/components/ECharts.vue";
import axios from "@/assets/api/axios";
import chart from "@/components/chart";
import ChartoOption from "@/assets/api/chart";
export default {
  data: function() {
    return {
      pagedata: null,
      chartoption: null,
      xAxisMonth: [],
      barData: [],
      lineData: []
    };
  },
  created: function() {
    this.getEnvData();
  },
  methods: {
    getEnvData() {
      var setting = {
          method: "POST",
          url: "/EarthData/getData"
        },
        data = {
          city: "定兴县",
          handel: "envir"
        };
      axios(
        res => {
          if (res.stutas === 200) {
            let data = res.data[0];
            this.pagedata = this.setPeoData(data);
            this.setOption(data);
          }
        },
        { setting, data }
      );
    },
    setPeoData(data) {
      return {
        option: parseInt(data.altitudeCenter) || parseInt(data.altitudeAverage),
        chart: {
          list: [
            parseInt(data.altitudeL),
            parseInt(data.altitudeAverage),
            parseInt(data.altitudeH)
          ]
        }
      };
    },
    setOption(chartdata) {
      let that = this;
      let data = {
        chart: [
          {
            month: "最低海拔",
            value: chartdata.altitudeL
          },

          {
            month: "平均海拔",
            value: chartdata.altitudeAverage
          },

          {
            month: "最高海拔",
            value: chartdata.altitudeH
          }
        ]
      };
      for (let i = 0; i < data.chart.length; i++) {
        that.xAxisMonth.push(data.chart[i].month);
        that.barData.push({
          name: that.xAxisMonth[i],
          value: data.chart[i].value
        });
        that.lineData.push({
          name: that.xAxisMonth[i]
        });
      }
      this.chartoption = {
        // backgroundColor: "#020d22",
        backgroundColor: "rgba(0,0,0,0)",
        title: "",
        grid: {
          top: "24%",
          left: "7%",
          bottom: "6%",
          containLabel: true
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "none"
          },
          formatter: function(params) {
            return (
              params[0]["data"].name +
              "<br/>" +
              params[1]["data"].value +
              "米" +
              "<br/>"
            );
          }
        },
        xAxis: [
          {
            type: "category",
            show: false,
            data: ["最低海拔", "平均海拔", "最高海拔"],
            axisLabel: {
              textStyle: {
                color: "#b6b5ab"
              }
            }
          },
          {
            type: "category",
            position: "bottom",
            data: that.xAxisMonth,
            boundaryGap: true,
            // offset: 40,
            axisTick: {
              show: false
            },
            axisLine: {
              show: false
            },
            axisLabel: {
              textStyle: {
                color: "#b6b5ab"
              }
            }
          }
        ],
        yAxis: [
          {
            show: true,
            offset: 52,
            splitLine: {
              show: false,
              lineStyle: {
                color: "rgba(255,255,255,0.2)"
              }
            },
            axisTick: {
              show: false
            },
            axisLine: {
              show: true
            },
            axisLabel: {
              show: true,
              color: "#b6b5ab"
            }
          },
          {
            show: false,
            type: "value",
            // name: "合格率(%)",
            nameTextStyle: {
              color: "#ccc"
            },
            axisLabel: {
              color: "#ccc"
            },
            splitLine: {
              show: false
            },
            axisLine: {
              show: false
            },
            axisTick: {
              show: false
            }
          }
        ],
        color: ["#e54035"],
        series: [
          {
            name: "海拔",
            type: "pictorialBar",
            xAxisIndex: 1,
            barCategoryGap: "-35%",
            // barCategoryGap: '-5%',
            symbol: 'path://d="M150 50 L130 130 L170 130  Z"',
            itemStyle: {
              normal: {
                color: function(params) {
                  let colorList = [
                    "rgba(13,177,205,0.8)",
                    "rgba(29,103,182,0.6)",
                    "rgba(13,177,205,0.8)",
                    "rgba(29,103,182,0.6)",
                    "rgba(13,177,205,0.8)",
                    "rgba(29,103,182,0.6)"
                  ];
                  return colorList[params.dataIndex];
                }
              },
              emphasis: {
                opacity: 1
              }
            },
            data: that.barData
          },
          {
            symbol:
              "image://",
            symbolSize: 42,
            name: "海拔",
            type: "line",
            yAxisIndex: 1,
            data: that.lineData,
            itemStyle: {
              normal: {
                borderWidth: 5,
                color: {
                  colorStops: [
                    {
                      offset: 0,
                      color: "#821eff"
                    },
                    {
                      offset: 1,
                      color: "#204fff"
                    }
                  ]
                }
              }
            }
          }
        ]
      };
    }
  },
  components: {
    chart
  }
};
</script>

<style>
.environmentchart
{
  background-size: 100% 100%;
}
.main
  {
    height: calc(100% - 120px);
    width: 100%;
    transition: all 0.5s linear;
}
.data-title-text > span{
    font-size: 2rem;
    font-weight: 800;
}
.data-title{
    background-color: #fff0;
    margin: 20px auto;
    line-height: 45px;
}
</style>

